﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;
using System.Runtime.Remoting;
using FiveStone.MyStack;


namespace FiveStone
{
    public partial class Online : Form
    {
        
        public Online()
        {            
            InitializeComponent();
        }

        private void btJoin_Click(object sender, EventArgs e)
        {
            if (txbIP.Text.Trim() == string.Empty || txbPort.Text.Trim() == string.Empty)
            {
                MessageBox.Show("主机IP和端口不能为空！", "提示");
            }
            else
            {                
                string ip = txbIP.Text.Trim();                
                bool isConnect = SocketHelper.Connection(ip);
                if (isConnect)
                {
                    User.isMeFirst = false;
                    MessageBox.Show("加入游戏成功！");                    
                    DialogResult = DialogResult.Yes;
                }
                else
                    MessageBox.Show("加入失败!");                
            }
        }

        /// <summary>
        /// 退出;
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btCancel_Click(object sender, EventArgs e)
        {
            SocketHelper.Close();
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void Online_Load(object sender, EventArgs e)
        {
            lbIP.Text = txbIP.Text = Config.GetLocalIP().ToString();
            lbPort.Text = txbPort.Text = Config.PORT.ToString();

            //MySocket.IP = lbIP.Text;
            //MySocket.Port = lbPort.Text;     
        }                
                
        private void btCreate_Click(object sender, EventArgs e)
        {
            //MySocket.RunTcpListen();
            btCreate.Enabled = false;
            lbWait.Visible = true;
            //while (!MySocket.tcpListen)
            //{
            //    Program.isOnline = true;
            //    this.Close();
            //}
            //远程化游戏对象
            //RemotingConfiguration.Configure(
            //    AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            //获得游戏对象的IP地址
            //Uri uri = new Uri(Boards.BuildRemotingUrl("localhost"));
            //获得游戏的远程对象
            //MySocket.RunTcpListen();
            //MessageBox.Show("创建主机成功！");
            SocketHelper.StartListen();
            User.isMeFirst = true;
            DialogResult = DialogResult.OK;
            MessageBox.Show("创建主机成功！");
            Close();

        }        

        public static void setTab(){
            //Online.tabPage2.Select();
        }


        public static void setStatus(string msg)
        {
            //Online.lbWait.Text = msg;
        }

    }
}
